<?php

/**
 * 频道
 */
require_once('includes/global.php');
require_once(ROOT_PATH . 'languages/' . $config['site_language'] . '/front.php');
require_once('includes/front.php');

if (isset($_GET['category_id'])) {
    $category_id = intval($_GET['category_id']);
} else {//推荐产品
    $category_id = 0;
}
//第几页
if (isset($_GET['page'])) {
    $page = intval($_GET['page']);
} else {
    $page = 1;
}

$smarty = new smarty();
smarty_header(true);
/* $cache_id = sprintf('%X', crc32(md5($channel_id.($category_id>0?"-".$category_id:'').(isset($_GET['page'])?intval($_GET['page']):1))));
  if (!$smarty->is_cached('wedo.html',$cache_id)){
  $parameters=array();
  $parameters['id']=$channel_id;
  if($category_id>0){
  $parameters['category_id']=$category_id;
  }
  $smarty->assign('here',here('channel',$parameters));
  } */
$cates = get_cateid();
$smarty->assign("tabs", $cates['cate']); //产品分类信息id和name
if($category_id == 0) $smarty->assign("tuijian",true);
$smarty->assign("contents",get_content_list());
$smarty->display('wedo.html', $cache_id);


//获取频道内容列表
function get_content_list() {
    $category_id = isset($_GET['category_id']) ? intval($_GET['category_id']) : 0;
    $content_list = array();

    if ($category_id == 0) {//没有具体的就拿全部推荐的
        $cates = get_cateid();
        $sql = "select * from ".$GLOBALS['db_prefix']."content where content_state=1 and content_is_best=1 and channel_id=2 and category_id in (".$cates['ids'].")";
    } else {//指定的
        $content_list = array();
        $sql = "select * from ".$GLOBALS['db_prefix']."content where content_state=1 and channel_id='2' and category_id=$category_id"; //某个类别下的产品
    }
    $page_size = 12;
    $page_current = isset($_GET['page']) ? intval($_GET['page']) : 1;
    $count = $GLOBALS['db']->getcount($sql);
    $res = $GLOBALS['db']->getall($sql." order by content_id desc limit ".(($page_current - 1) * $page_size).",".$page_size);
    if ($count > 0) {
        $no = $count - (($page_current - 1) * $page_size);
        foreach ($res as $row) {//缩略图，标题，简介，日期,id
            $content_list[$row['content_id']]['id'] = $row['content_id'];
            $content_list[$row['content_id']]['title'] = $row['content_title'];
            $content_list[$row['content_id']]['des'] = $row['content_description'];
            $content_list[$row['content_id']]['img'] = $row['content_thumb'];
            $content_list[$row['content_id']]['date'] = date("Y-m-d", $row['content_time']); //xxxx-xx-xx
        }
        $pagebar = get_page($page,$category_id, ceil($count/$page_size));
    } else {
        $pagebar = "";
    }
    $GLOBALS['smarty']->assign('pagebar', $pagebar);
    return $content_list;
}

function get_page($page_current, $category_id, $pages) {
    $paging = "";
    $paging .="<div>";
    $paging .= $page_current==1?"<a disabled>上一页</a>":"<a href='wedo.php?category_id=".$category_id."&page=".($page_current-1)."'>上一页</a>";
    for($i=1;$i<=$pages;$i++){
        $paging.='<a href="wedo.php?category_id='.$category_id.'&page='.$i.'">$i</a>';
    }
    $paging = $paging = $page_current==$pages?"<a disabled>下一页</a>":"<a href='wedo.php?category_id=".$category_id."&page=".($page_current+1)."'>下一页</a>";
    $paging .= "</div>";
}

function get_cateid() {
    /*
    $ids = "";
    $temp1 = array();
    $temp2 = array();
    $array = array();
    $res = $GLOBALS['db']->getall("select * from ".$GLOBALS['db_prefix']."content_category where channel_id=2 and parent_id=6");
    if ($res) {
        $i = 0;
        foreach ($res as $item) {
            $temp1[$i]['id'] = $item['category_id'];
            $temp2[$i]['id'] = $item['category_id'];
            $temp2[$i]['name'] = $item['category_name'];
            $i++;
        }
        $ids = implode(",", $temp1);

    }
    $array['ids'] = $ids;
    $array['cate'] = $temp2;
    */
    $res = $GLOBALS['db']->getall("select * from ".$GLOBALS['db_prefix']."content_category where channel_id=2 and parent_id=6 limit 5");
    $cid = isset($_GET['category_id']) ? intval($_GET['category_id']) : 0;
    foreach($res as $key=> $val){
        $ids[]=$val['category_id'];
        $res[$key]['current']=$val['category_id']==$cid?'current':'';
    }
    $arr['ids']=implode(",", $ids);
    $arr['cate']=$res;
    //dump($arr);
    return $arr;
}

?>